package com.goktech.olala.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goktech.olala.core.config.SysConfig;
import com.goktech.olala.core.req.ReqBusiness;
import com.goktech.olala.core.resp.RespBusinessVo;
import com.goktech.olala.core.service.IBusinessService;
import com.goktech.olala.server.dao.goods.BusinessMapper;
import com.goktech.olala.server.pojo.goods.Business;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("businessService")
public class BusinessServiceImpl implements IBusinessService {
    @Autowired
    BusinessMapper businessMapper;

    //查询店铺信息
    @Override
    public PageInfo<RespBusinessVo> queryBusiness(int pageIndex, int pageSize, ReqBusiness reqBusiness) throws Exception {
        PageHelper.startPage(pageIndex, pageSize);
        List<RespBusinessVo> infoList = businessMapper.selectByExample(reqBusiness);
        PageInfo<RespBusinessVo> pageInfo = new PageInfo(infoList);
        return pageInfo;
    }

    //添加店铺信息
    @Override
    public int saveBusiness(ReqBusiness reqBusiness) throws Exception {
        if (null == reqBusiness) {
            return 0;
        }
        String businessId = reqBusiness.getBusinessId();
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Business business = new Business();
        BeanUtils.copyProperties(reqBusiness, business);
        business.setUpdateBy(SysConfig.SYSTEM_USER);
        business.setUpdateTime(Timestamp.valueOf(nowTime));

        if(StringUtils.isNotBlank(businessId)){
            return businessMapper.updateByPrimaryKey(business);
        }
        businessId = String.valueOf(System.currentTimeMillis()/1000);
        long min = 1;
        long max = 20000;
        Long TerminaCode = RandomUtils.nextLong(min, max);
        //初始化数据
        business.setBusinessId(businessId);//初始化ID
        business.setCreateBy(SysConfig.SYSTEM_USER);
        business.setCreateTime(Timestamp.valueOf(nowTime));
        business.setStatus(1);
        business.setBusinessLevel(1);
        business.setFansCount(0);
        business.setScore(100);
        business.setTerminalCode(TerminaCode);//设置终端编码
        business.setCreateBy("店主大大");
        return businessMapper.insert(business);
    }

    /**
     * 根据主键查询店铺信息
     *
     * @param businessId
     * @return
     * @throws Exception
     */
    @Override
    public RespBusinessVo queryBusinessById(String businessId) throws Exception {
        if (null == businessId) {
            return null;
        }
        RespBusinessVo respBusinessVo = new RespBusinessVo();
        Business business = businessMapper.selectByPrimaryKey(businessId);
        BeanUtils.copyProperties(business, respBusinessVo);
        return respBusinessVo;
    }

    @Override
    public int removeBusinessById(String businessId) throws Exception {
        if (businessId != null) {
            return businessMapper.deleteByPrimaryKey(businessId);
        }
        return 0;
    }
}
